package com.ikun.auth.application.controller;



import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.ikun.auth.application.convert.AuthRoleDTOConverter;
import com.ikun.auth.application.convert.AuthUserDTOConverter;
import com.ikun.auth.application.dto.AuthRoleDTO;
import com.ikun.auth.application.dto.AuthUserDTO;
import com.ikun.auth.common.entity.Result;
import com.ikun.auth.domain.entity.AuthRoleBO;
import com.ikun.auth.domain.entity.AuthUserBO;
import com.ikun.auth.domain.service.AuthRoleDomainService;
import com.ikun.auth.domain.service.AuthUserDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
* @Description: 角色controller
* @Author: gqh
* @Date: 2024/5/18 12:22
*/
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {


    @Resource
    private AuthRoleDomainService authRoleDomainService;

    @RequestMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.add.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            //参数校验
            Preconditions.checkNotNull(authRoleDTO.getRoleKey(), "角色key不能为空");
            Preconditions.checkNotNull(authRoleDTO.getRoleName(), "角色名称不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            return Result.ok(authRoleDomainService.add(authRoleBO));
        } catch (Exception e) {
            log.error("UserController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增角色失败");
        }
    }

    @RequestMapping("/update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.update.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            //参数校验
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            return Result.ok(authRoleDomainService.update(authRoleBO));
        } catch (Exception e) {
            log.error("UserController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新角色信息失败");
        }
    }

    @RequestMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.delete.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            return Result.ok(authRoleDomainService.deleteById(authRoleBO));
        } catch (Exception e) {
            log.error("UserController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除角色信息失败");
        }
    }


}
